//
//  XLTextView.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/26.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

class XLTextView: UITextView {

    override init(frame: CGRect, textContainer: NSTextContainer?) {
          super.init(frame: frame, textContainer: textContainer)
        //设置界面
        setupUI()
        
    }
        
        
    
   
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func updateTipLabelContContra(view: UIView){
        
        
        tipLable.snp_remakeConstraints { (make) -> Void in
            
            make.right.equalTo(self).offset(-5)
            
            make.bottom.equalTo(view.snp_top).offset(-5)
            
        }
        
        
    }
    
    // MARK: - 私有方法
    
    @objc private func textxhangeDid(){
        
        placeHoderLable.hidden = hasText()
        
        let maxLenth = 140
        
        let len = maxLenth - text.characters.count
        
        tipLable.text = len.description
        
        tipLable.textColor = (len >= 0) ? UIColor.lightGrayColor() : UIColor.redColor()
        
    }
    
    
// MARK: - 占位文本
    
    private lazy var placeHoderLable: UILabel = UILabel(text: "分享新鲜事....",
        textColor: UIColor.lightGrayColor(),
        fontsize: 15)
    
    private lazy var tipLable: UILabel  = UILabel(text: "140", textColor: UIColor.lightGrayColor(), fontsize: 12)
    
    
    deinit{
        //一出通知
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidChangeNotification, object: nil)
    }
    
    
    
    
}
// MARK: - 设置界面
extension XLTextView{
    
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        font = UIFont.systemFontOfSize(15)
        
        textColor = UIColor.darkGrayColor()
        
        alwaysBounceVertical = true
        
        //接触键盘
        
        keyboardDismissMode = .OnDrag   
        
        let sizeView = UIView()
        
        sizeView.userInteractionEnabled = false
        
        insertSubview(sizeView, atIndex: 0)
        
        sizeView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
            make.size.equalTo(self)
            
        }
        
        
        //添加控件
        
        addSubview(placeHoderLable)
        //自动布局
        placeHoderLable.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self).offset(5)
            
            make.top.equalTo(self).offset(8)
            
            make.width.equalTo(self).offset(-10)

        }
        
        addSubview(tipLable)
        
        tipLable.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(self).offset(-5)
            make.bottom.equalTo(self)
            
        }
        
        //添加通知
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textxhangeDid", name: UITextViewTextDidChangeNotification, object: nil)
        
        
    }
 

}
